package com.itheima.controller;

import com.itheima.Util.JwtUtils;
import com.itheima.pojo.Emp;
import com.itheima.pojo.Result;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@RestController
public class LoginController {
    @Autowired
    private EmpService empService;

    @PostMapping("/login")
    public Result login(@RequestBody Emp emp) {
        Emp e = empService.login(emp);
        if (e != null) {
            HashMap<String, Object> stringObjectHashMap = new HashMap<>();
            stringObjectHashMap.put("id", e.getId());
            stringObjectHashMap.put("username", e.getUsername());
            stringObjectHashMap.put("name", e.getName());
            String s = JwtUtils.generateJwt(stringObjectHashMap);
            return Result.success(s);
        }
        return Result.error("用户名或密码错误");
    }
}
